using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class MissionButton : MonoBehaviour
{
    private int _index;
    private Action<int> _action;

    private Image _select;
    private Transform _lock;
    private Image _icon;
    private Transform _stars;

    private TextMeshProUGUI _missionTitleText;
    private TextMeshProUGUI _rewardText;

    public void Init(int index, Action<int> action)
    {
        _index = index;
        GetComponent<Button>().onClick.AddListener(OnClick);

        _select = transform.Find("Select").GetComponent<Image>();
        _lock = transform.Find("Lock");
        _icon = transform.Find("Icon").GetComponent<Image>();
        _missionTitleText = transform.Find("MissionTitleText").GetComponent<TextMeshProUGUI>();
        _rewardText = transform.Find("RewardText").GetComponent<TextMeshProUGUI>();
        _stars = transform.Find("Stars");

        _missionTitleText.text = $"{GameManager.Instance._missionDatas[index].MissionName}";
        _rewardText.text = $"奖励：￥{Constants.MissonRewrd[index]}";
        
        for (int i = 0; i < _stars.childCount; i++)
        {
            _stars.GetChild(i).gameObject.SetActive(i + 1 <= GameManager.Instance._missionDatas[index].Stars);
        }

        if (!GameManager.Instance._missionDatas[index].IsUnlock)
        {
        }

        _icon.sprite = GameManager.Instance.LoadSprite($"{Constants.PathMissonSprite}{index}");

        _action = action;
        ShowBtnAnim(true);
    }

    public void SetSelect(int index, bool isUnlock)
    {
        _select.gameObject.SetActive(_index == index);
        _lock.gameObject.SetActive(!isUnlock);

        if (!isUnlock)
        {
        }
        else
        {
            int curIndex = GameManager.Instance.GetCurMissonIndex();
            if (curIndex == -1)
            {
            }
            else
            {
                if (_index == curIndex)
                {
                }
                else
                {
                }
            }
        }
    }

    void OnClick()
    {
        _action?.Invoke(_index);
    }

    public void ShowBtnAnim(bool isShow)
    {
        this.DOKill();

        if (isShow)
        {
            transform.localScale = Vector3.zero;
            transform.DOScale(Vector3.one, 0.5f).SetEase(Ease.OutElastic);
        }
        else
        {
            transform.localScale = Vector3.one;
        }
    }

    public void SetLockImg(bool isShowLock) => _lock.gameObject.SetActive(isShowLock);
}